﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Web.Common;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.PFood
{
    public partial class PFoodEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_PData tPData = new T_PData();              //Vc检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());
            List<E_PData> ePDataList = tPData.GetModelList("RawID=" + RawId + " and FoodTime=cast('" + date.ToString() + "' as datetime)");

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 15;
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (ePDataList != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_PData ePData = ePDataList.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (ePData != null)
                {
                    this.txt_P.Text = ePData.P;
                    this.txt_V4.Text = ePData.V4;
                    this.txt_M.Text = ePData.M;
                    this.txt_V1.Text = ePData.V1;
                    this.txt_V2.Text = ePData.V2;
                    this.txt_W.Text = ePData.Result;
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                double P = Convert.ToDouble(this.txt_P.Text.Trim());
                double V4 = Convert.ToDouble(this.txt_V4.Text.Trim());
                double M = Convert.ToDouble(this.txt_M.Text.Trim());
                double V1 = Convert.ToDouble(this.txt_V1.Text.Trim());
                double V2 = Convert.ToDouble(this.txt_V2.Text.Trim());

                double Result = P * V4 / M * V1 / V2 * 0.0001;
                Convert.ToInt32(Result);

                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                E_PData ePData = new E_PData();
                ePData.RawID = RawId;
                ePData.RawName = eRawMaterial.RAWName;
                ePData.P = P.ToString();
                ePData.V4 = V4.ToString();
                ePData.M = M.ToString();
                ePData.V1 = V1.ToString();
                ePData.V2 = V2.ToString();
                ePData.Result = Result.ToString("f2");
                ePData.FoodTime = date;
                ePData.FoodNum = FoodNum;
                tPData.RemoveByFoodNum(ePData);   //删除现有的检测记录
                tPData.Add(ePData);               //添加检测记录
                this.txt_W.Text = ePData.Result;
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            List<E_PData> ePDataList = tPData.GetModelList("RawID=" + RawId + " and FoodTime=cast('" + date.ToString() + "' as datetime)");


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 15;
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (ePDataList == null || ePDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            ePDataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.Result);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / ePDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / ePDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}